import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/event/wms_page_event.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/nestedscroll/nested_scroll_view_inner_scroll_position_key_widget.dart'
    as extended;
import 'package:library_base/widget/tab/round_indicator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/entities/event/dispatcher_goods_count.dart';
import 'package:module_wms/page/outbound_orders/as_goods/as_goods_page.dart';
import 'package:module_wms/page/outbound_orders/as_order/as_order_page.dart';
import 'package:module_wms/page/outbound_orders/outbound_orders_manage_controller.dart';
import 'package:library_base/entities/event/page_refresh_event.dart';

class OutboundOrdersManagePage extends StatefulWidget {
  const OutboundOrdersManagePage({Key? key}) : super(key: key);
  
  @override
  State<OutboundOrdersManagePage> createState() => _OutboundOrdersManagePageState();
}

class _OutboundOrdersManagePageState extends BaseState<OutboundOrdersManagePage, OutboundOrdersManageController>
    with SingleTickerProviderStateMixin {
  final pageController = PageController();

  late TabController _tabController;

  final List<String> _tabTitles = [
    MultiLanguage.asOrder.tr,
    MultiLanguage.asGoods.tr,
  ];

  @override
  void onCreate() {
    setNavigationBarVisible(false);
    setStatusBarVisible(true);
    setStatusBarColor(ThemeUtil.getTheme().colorScheme.secondary);
    _tabController = TabController(length: 2, vsync: this);
    Event.eventBus.on<DispatcherGoodsCount>().listen((event) {
      if (event.page == 0) {
        baseGetxController!.orderCount.value = event.count;
      } else {
        baseGetxController!.goodsCount.value = event.count;
      }
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  void controllerOnCreate() {
    super.controllerOnCreate();
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return Column(
      children: <Widget>[
        Row(
          children: [
            GestureDetector(
                onTap: (){ Get.back(); },
                child: Padding(
                    padding: EdgeInsets.only(left: 24.w,top:10.w,bottom: 10.w),
                    child: LocalImage('black_leftward_arrow',package: Constant.packLib(),width: 36.w,height: 36.w))),
            Container(
              height: 100.w,
              alignment: Alignment.centerLeft,
              child: TabBar(
                isScrollable: true,
                controller: _tabController,
                labelColor: ThemeUtil.getTheme().primaryColor,
                labelStyle: normalBoldTextColor32,
                unselectedLabelColor: ThemeUtil.getTheme().normalTextColor(),
                unselectedLabelStyle: normalTextColor28,
                indicatorSize: TabBarIndicatorSize.label,
                indicator: RoundTabIndicator(
                    borderSide:
                    BorderSide(color: ThemeUtil.getTheme().primaryColor, width: 3),
                    isRound: true,
                    insets: EdgeInsets.only(left: 20.r, right: 20.r)),
                onTap: (index) {
                  pageController.animateToPage(index, duration: const Duration(milliseconds: 300), curve: Curves.ease);
                  baseGetxController!.update();
                },
                tabs: <Tab>[
                  Tab(text: _tabTitles[0]),
                  Tab(text: _tabTitles[1]),
                ],
              ),
            ),
            Expanded(
              child: GestureDetector(
                onTap: showTimePicker,
                child: GetBuilder<OutboundOrdersManageController>(
                  assignId: true,
                  builder: (logic) {
                    return Row(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Visibility(
                            visible: true,
                            child: Text(
                                MultiLanguage.productsInTotal.trArgs([
                                  "${_tabController.index == 0 ? baseGetxController!.orderCount : baseGetxController!.goodsCount}"
                                ]),
                                style: sixthTextColor26),
                          ),
                          gapW15,
                          LocalImage('common_filter',
                              package: Constant.packLib(), width: 38.w, height: 38.w),
                          gapW24,
                        ]);
                  },
                ),
              ),
            ),
          ],
        ),
        Expanded(
        child: PageView.builder(
            physics: const BouncingScrollPhysics(),
            controller: pageController,
            onPageChanged: (index){
              _tabController.animateTo(index, duration: const Duration(milliseconds: 300));
              baseGetxController!.update();
            },
            itemCount: _tabTitles.length,
            itemBuilder: (context, index) {
              if(index==0){
                return extended.NestedScrollViewInnerScrollPositionKeyWidget(
                    Key(_tabTitles[0]), const AsOrderPage());
              }
              return extended.NestedScrollViewInnerScrollPositionKeyWidget(
                  Key(_tabTitles[1]), const AsGoodsPage());
            }
        )),

        // Expanded(
        //     child: ExtendedTabBarView(
        //       controller: _tabController,
        //       pageController: pageController,
        //       children: <Widget>[
        //         extended.NestedScrollViewInnerScrollPositionKeyWidget(
        //             Key(_tabTitles[0]), const AsOrderPage()),
        //         extended.NestedScrollViewInnerScrollPositionKeyWidget(
        //             Key(_tabTitles[1]), const AsGoodsPage()),
        //       ],
        //     ))
      ],
    );
  }

  showTimePicker() {
    Event.eventBus.fire(WmsPageEvent(_tabController.index));
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    super.dispose();
    pageController.dispose();
    _tabController.dispose();
  }

  @override
  OutboundOrdersManageController registerController() => Get.find<OutboundOrdersManageController>();

  @override
  void onResume() {
    super.onResume();
    if(_tabController.index == 0){
      Event.eventBus.fire(AsOrderPageRefreshEvent());
    }else if(_tabController.index == 1){
      Event.eventBus.fire(AsGoodsPageRefreshEvent());
    }
  }

}
